import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';

import 'package:ball_lion/resources/colours.dart';
import 'wqs_text.dart';
import 'wqs_button.dart';

enum DialogStyle { alert, single, select, alertButton, singleButton }

class QsDialog extends StatefulWidget {
  final String title;
  final String? content;
  final Widget? contentWidget;
  final TextAlign? contentAlign;
  final String? cancelText;
  final String? confirmText;
  final int? confirmTime;
  final Axis buttonDirection;
  final DialogStyle style;
  final List<String>? selectList;

  ///双选框
  const QsDialog.alert(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText = '取消',
      this.contentAlign,
      this.confirmTime})
      : buttonDirection = Axis.horizontal,
        selectList = null,
        style = DialogStyle.alert;

  ///双选框按钮风格
  const QsDialog.alertBUTTON(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText = '取消',
      this.contentAlign,
      this.buttonDirection = Axis.horizontal,
      this.confirmTime})
      : selectList = null,
        style = DialogStyle.alertButton;

  ///单双选框按钮风格
  const QsDialog.singleBUTTON(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText,
      this.contentAlign,
      this.confirmTime})
      : buttonDirection = Axis.horizontal,
        selectList = null,
        style = DialogStyle.singleButton;

  ///单双选框
  const QsDialog.single(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.confirmText = '确定',
      this.cancelText,
      this.contentAlign,
      this.confirmTime})
      : buttonDirection = Axis.horizontal,
        selectList = null,
        style = DialogStyle.single;

  ///多选框
  const QsDialog.select(this.title,
      {super.key,
      this.content,
      this.contentWidget,
      this.cancelText = '取消',
      this.contentAlign,
      this.selectList})
      : confirmTime = null,
        confirmText = null,
        buttonDirection = Axis.vertical,
        style = DialogStyle.select;

  @override
  State<QsDialog> createState() => _QsDialogState();
}

class _QsDialogState extends State<QsDialog> {
  late int _count = widget.confirmTime ?? 0;

  set count(int value) => setState(() => _count = value);

  @override
  void initState() {
    if (_count != 0) {
      Timer.periodic(const Duration(seconds: 1), (timer) {
        count = _count - 1;
        if (_count == 0) timer.cancel();
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      alignment: Alignment.center,
      backgroundColor: Colours.white,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(24))),
      child: PopScope(
        canPop: false,
        child: SizedBox(
            width: 280,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  padding: const EdgeInsets.fromLTRB(16, 24, 16, 26),
                  constraints: const BoxConstraints(minHeight: 110),
                  alignment: Alignment.center,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      if (widget.title != "")
                        QsText(widget.title,
                            textAlign: TextAlign.center,
                            fontSize: 17,
                            color: Colours.N9,
                            fontWeight: FontWeight.w500),
                      if (widget.content != null ||
                          widget.contentWidget != null) ...[
                        if (widget.title != "") Container(height: 16),
                        widget.content == null
                            ? DefaultTextStyle(
                                style: const TextStyle(
                                    fontSize: 14, color: Colours.N8),
                                child: widget.contentWidget!)
                            : QsText(widget.content!.split("").join("\u200B"),
                                textAlign:
                                    widget.contentAlign ?? TextAlign.left,
                                color: Colours.N8)
                      ]
                    ],
                  ),
                ),
                bottom(),
              ],
            )),
      ),
    );
  }

  Widget bottom() {
    switch (widget.style) {
      case DialogStyle.alert:
      case DialogStyle.single:
        return Container(
          alignment: Alignment.center,
          decoration: const BoxDecoration(
              border: Border(top: BorderSide(color: Colours.N3, width: 0.5))),
          height: 44,
          child: Row(children: [
            if (widget.cancelText != null) ...[
              Expanded(
                  child: QsText(widget.cancelText!,
                          textAlign: TextAlign.center,
                          fontSize: 16,
                          fontWeight: FontWeight.w500,
                          color: Colours.N9)
                      .center
                      .tap(() => Navigator.pop(context, false))),
              Container(width: 0.5, color: Colours.N3)
            ],
            Expanded(
                child: QsText(
                        "${widget.confirmText}${_count.hasValue ? "($_count秒)" : ""}",
                        textAlign: TextAlign.center,
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        color:
                            Colours.main.withOpacity(_count.hasValue ? 0.5 : 1))
                    .center
                    .tap(() =>
                        _count.hasValue ? null : Navigator.pop(context, true)))
          ]).paddingSymmetric(horizontal: 16),
        );
      case DialogStyle.select:
        return Column(
            children: List.generate(
                (widget.selectList?.length ?? -1) + 1,
                (index) => Container(
                        height: 44,
                        alignment: Alignment.center,
                        decoration: const BoxDecoration(
                            border: Border(
                                top:
                                    BorderSide(color: Colours.N3, width: 0.5))),
                        child: QsText(
                            index == widget.selectList?.length
                                ? widget.cancelText!
                                : widget.selectList?[index],
                            fontSize: 16,
                            color: index == widget.selectList?.length
                                ? Colours.N9
                                : Colours.main,
                            fontWeight: FontWeight.w500))
                    .tap(() => Navigator.pop(
                        context,
                        index == widget.selectList?.length
                            ? false
                            : index)))).paddingSymmetric(horizontal: 16);
      case DialogStyle.alertButton:
      case DialogStyle.singleButton:
        if (widget.buttonDirection == Axis.horizontal) {
          return Row(children: [
            if (widget.cancelText != null) ...[
              Flexible(
                fit: FlexFit.tight,
                child: QsButton(
                  borderRadius: 4,
                  onPressed: () => Navigator.pop(context, false),
                  backgroundColor: Colours.greyF2,
                  foregroundColor: Colours.N9,
                  text: widget.cancelText,
                ),
              ),
              const SizedBox(width: 10),
            ],
            Flexible(
              fit: FlexFit.tight,
              child: QsButton(
                borderRadius: 4,
                onPressed: () => Navigator.pop(context, true),
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                text: widget.confirmText,
              ),
            )
          ]).paddingOnly(bottom: 24, left: 16, right: 16);
        } else {
          return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
            QsButton.large(
              borderRadius: 4,
              onPressed: () => Navigator.pop(context, true),
              backgroundColor: Colours.main,
              foregroundColor: Colours.white,
              text: widget.confirmText,
            ),
            QsButton.large(
              borderRadius: 4,
              onPressed: () => Navigator.pop(context, false),
              foregroundColor: Colours.N9,
              backgroundColor: Colours.white,
              text: widget.cancelText,
            ),
            const SizedBox(height: 4)
          ]).paddingSymmetric(horizontal: 16);
        }
    }
  }
}
